package algorithm.子数组达到规定累加和的最大长度系列问题.转圈问题;

public class ZigZagPrintMatrix {

    public static void printMatrixZigZag(int[][] arr){
        int length1 = arr.length;
        int length2 = arr[0].length;
        int index1 = 0;
        boolean index1_flag = true;
        boolean index2_flag = true;
        int index2 = 0;
        boolean flag = true;
        for (int i = 0; i < length1 + length2 - 1; i++) {
            if(index1 == length1 && index1_flag){
                index1 = 1;
                index1_flag = false;
            }
            if(index2 == length2 && index2_flag){
                index2 = 1;
                index2_flag = false;
            }
            int index1_a = index1_flag? index1 : length1 - 1;
            int index1_b = index1_flag? 0 : index1;
            int index2_a = index2_flag? 0 : index2;
            int index2_b = index2_flag? index2 : length2 - 1;
            printLine(arr, index1_a, index1_b, index2_a, index2_b, flag);
            index1++;
            index2++;
            flag = !flag;
        }


    }

    public static void printLine(int[][] arr, int index1_a, int index1_b, int index2_a, int index2_b, boolean flag){
        if(flag){
            while(index1_a != index2_a){
                System.out.println(arr[index1_a--][index1_b++]);
            }
            System.out.println(arr[index1_a][index1_b]);
        }else {
            while(index1_a != index2_a){
                System.out.println(arr[index2_a++][index2_b--]);
            }
            System.out.println(arr[index2_a][index2_b]);
        }
    }

    public static void main(String[] args) {
        int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
        printMatrixZigZag(matrix);
    }
}
